事情的起因是我在看下面一段代码遇到的疑惑,明明是while True,为什么代码没有死循环??
class D(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
url = self.queue.get()
self.download_file(url)
self.queue.task_done()
def download_file(self, url):
h = urllib2.urlopen(url)
f = os.path.basename(url)+'.html'
with open(f,'wb') as f:
while True:
c = h.read(1024)
if not c:
break
f.write(c)
if __name__ == "__main__":
urls= ['http://www.baidu.com','http://www.sina.com']
queue = Queue.Queue()
for i in range(5):
t = D(queue)
t.setDaemon(True)
t.start()
for u in urls:
queue.put(u)
queue.join()